package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Voice;
import com.tanhua.domain.mongo.VoiceTime;
import com.tanhua.dubbo.api.mongo.VoiceApi;
import com.tanhua.dubbo.utils.IdService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Random;

/**
 * wzp 2021/6/23 11:22
 */
@Service
public class VoiceApiImpl implements VoiceApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdService idService;

    @Override
    public void save(Voice voice) {
        //设置自增长vid，并存入语音
        voice.setVid(idService.getNextId("voice"));
        mongoTemplate.save(voice);
    }

    @Override
    public void recover() {
        //将用户语音接收次数恢复为10次
        Query query = new Query();
        Update update = new Update();
        update.set("time",10);
        mongoTemplate.updateMulti(query,update,VoiceTime.class);
    }

    @Override
    public Integer timesSubtract(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId.intValue()));
        VoiceTime voiceTime = mongoTemplate.findOne(query, VoiceTime.class);
        if (voiceTime==null){
            //查询结果为空时，插入新用户，添加9次使用机会
            VoiceTime vt = new VoiceTime();
            vt.setId(new ObjectId());
            vt.setUserId(userId.intValue());
            vt.setTime(9);
            mongoTemplate.save(vt);
        }
        Integer time = voiceTime.getTime();
        if (time!=0){
            //查询次数不为0时，设置数据库数值减1
            Update update = new Update();
            update.set("time",--time);
            mongoTemplate.updateFirst(query,update,VoiceTime.class);
        }
        return time;
    }

    @Override
    public Voice findVoice(Long userId) {
        //条件是排除当前用户id，语音状态为1
        Query query = new Query(Criteria.where("userId").ne(userId).and("state").is(1));
        //查询状态为1且不是当前用户语音的数量count
        long count = mongoTemplate.count(query, Voice.class);
        //获得一个在count以内的随机数
        int number=0;
        if (count!=0) {
            Random random = new Random();
            number = random.nextInt((int) count);
        }
        //跳过随机数，获得一个随机的用户语音
        query.skip(number);
        List<Voice> voices = mongoTemplate.find(query, Voice.class);
        if (voices!=null && voices.size()>0){
            //获得语音后，需要将当前的语音状态设置为0，即为不可用语音
            query=new Query(Criteria.where("vid").is(voices.get(0).getVid()));
            Update update = new Update();
            update.set("state",0);
            mongoTemplate.updateFirst(query,update,Voice.class);
        }else {
            //语音为空时，添加一个语音对象，避免报错
            voices.add(new Voice());
        }
        return voices.get(0);
    }
}
